package com.estronger.shopmanage.ui.aftermarket;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.BeanBase;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.aftermarket.bean.AftermarketDetailBean;
import com.estronger.shopmanage.ui.aftermarket.bean.AftermarketItem;
import com.estronger.shopmanage.ui.aftermarket.views.GoodsView;
import com.estronger.shopmanage.ui.measure.MeasureActivity;
import com.estronger.shopmanage.utils.TimeUtils;
import com.estronger.shopmanage.view.AppDialog;
import com.lidroid.xutils.http.RequestParams;

/*
* 售后预约完工确认
* */
public class AppointmentSureActivity extends BaseActivity implements CheckBox.OnCheckedChangeListener{

    public static String send_deliv_id;
    public static Integer CALL_RESULT;

    @BindView(R.id.confirm_name_real)
    TextView tv_client;
    @BindView(R.id.confirm_actual_time)
    TextView tv_actual_time;
    @BindView(R.id.confirm_other_time)
    TextView tv_other_time;

    @BindView(R.id.actual_cb)
    CheckBox cb_actual;
    @BindView(R.id.other_time_cb)
    CheckBox cb_other;
    @BindView(R.id.no_sure_cb)
    CheckBox cb_no_sure;

    @BindView(R.id.confirmdesc)
    EditText et_confirmdesc;

    @BindView(R.id.good_view)
    GoodsView goodsView;

    @BindView(R.id.ok)
    Button okBtn;
    @BindView(R.id.cancel)
    Button cancelBtn;

    AftermarketItem detainInfoBean;

    @Override
    protected void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = View.inflate(this, R.layout.activity_appointment_sure_layout,null);
        setContentView(view);
        ButterKnife.bind(this,view);

        cb_actual.setOnCheckedChangeListener(this);
        cb_other.setOnCheckedChangeListener(this);
        cb_no_sure.setOnCheckedChangeListener(this);

        requestDetail();
    }

    void onBindData(){
        tv_client.setText(detainInfoBean.getFcstname());
        tv_actual_time.setText(detainInfoBean.getFplantime());
        tv_other_time.setText(TimeUtils.formatTime(System.currentTimeMillis(),
                ""));
        goodsView.setDataArray(detainInfoBean.getOrditem());
    }

    void requestDetail(){
        String taskId = getIntent().getStringExtra("taskId");
        RequestParams params = new RequestParams();
        params.addBodyParameter("sf_action", "claimgettaskdetail");
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        params.addBodyParameter("taskid", taskId);
        showDialog("获取任务详情中",true);
        connect(params, NetManager.AFTERMARKET_DETAIL);
    }

    @Override
    protected void onNetSuccess(int code, String result) {
        super.onNetSuccess(code, result);
        if (code == NetManager.AFTERMARKET_DETAIL){
            AftermarketDetailBean detailBean = gson.fromJson(result,AftermarketDetailBean.class);
            detainInfoBean = detailBean.getData();
            onBindData();
            Log.e("AppointmentSureActivity",result);
        }else if (code == NetManager.AFTERMARKET_CONFIR_APP){
            BeanBase bean = gson.fromJson(result, BeanBase.class);
            if (bean.success) {
                com.estronger.shopmanage.utils.Toast.show(this, "提交成功", 2000);
                setResult(AftermarketActivity.REFLESH_DATA);
                finish();
            } else {
                com.estronger.shopmanage.utils.Toast.show(this, bean.msg, 2000);
            }
        }
    }

    // 弹出时间选择器
    public void showPicker(View view) {
        if (view.getId() == R.id.confirm_picker1) {
            AppDialog.createPicker(this, tv_actual_time).show();
        } else if (view.getId() == R.id.confirm_picker2) {
            AppDialog.createPicker(this, tv_other_time).show();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.actual_cb:
                if (isChecked) {
                    cb_no_sure.setChecked(false);
                    cb_other.setChecked(false);
                }
                break;
            case R.id.other_time_cb:
                if (isChecked) {
                    cb_no_sure.setChecked(false);
                    cb_actual.setChecked(false);
                }
                break;
            case R.id.no_sure_cb:
                if (isChecked) {
                    cb_actual.setChecked(false);
                    cb_other.setChecked(false);
                }
                break;
        }
    }

    public void close(View v) {
        finish();
    }

    /*
    * 保存
    * */
    public void save(View view) {
        String time = "";
        String datetype = "0";
        if (cb_other.isChecked()){
            datetype = "1";
            time = tv_other_time.getText().toString().trim();
            if (TextUtils.isEmpty(time)){
                Toast.makeText(this,"请选择新的预约上门时间",Toast.LENGTH_LONG).show();
                return;
            }
        }else if (cb_no_sure.isChecked()){
            datetype = "2";
        }
        String desCotnent = et_confirmdesc.getText().toString().trim();
        String taskId = getIntent().getStringExtra("taskId");
        RequestParams params = new RequestParams();
        params.addBodyParameter("sf_action", "claimconfirmfinish");
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        params.addBodyParameter("taskid", taskId);
        params.addBodyParameter("datetype",datetype);
        params.addBodyParameter("newplantime",time);
        params.addBodyParameter("plandesc",desCotnent);
        showDialog("数据提交中...", true);
        connect(params, NetManager.AFTERMARKET_CONFIR_APP);
    }

}
